/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.permission.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.legendshop.dao.support.CriteriaQuery;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.dao.support.SimpleSqlQuery;
import com.legendshop.framework.commond.ClientException;
import com.legendshop.framework.commond.JCFException;
import com.legendshop.framework.commond.Request;
import com.legendshop.framework.commond.Response;
import com.legendshop.framework.commond.State;
import com.legendshop.framework.commond.facade.AbstractBizDelegate;
import com.legendshop.framework.commond.facade.DelegateUtil;
import com.legendshop.oa.constants.SysParameterEnum;
import com.legendshop.oa.model.Function;
import com.legendshop.oa.model.LoginHistory;
import com.legendshop.oa.model.Permission;
import com.legendshop.oa.model.Role;
import com.legendshop.oa.model.User;
import com.legendshop.oa.model.UserRole;
import com.legendshop.oa.util.PropertiesUtil;
import com.legendshop.permission.common.ErrorCode;
import com.legendshop.permission.common.ServiceConsts;
import com.legendshop.permission.service.RightDelegate;
import com.legendshop.util.AppUtils;

/**
 * 
 * 权限管理代理
 */
public class RightDelegateImpl extends AbstractBizDelegate implements RightDelegate {

	/**
	 * Instantiates a new right delegate impl.
	 */
	public RightDelegateImpl() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#findWorkflow(com.legendshop.command
	 * .framework.State)
	 */

	public boolean findWorkflow(State state) {
		Request req = new Request();
		req.setServiceName("RunByResultProcessor");
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return true;
		} catch (Exception e) {
			DelegateUtil.handleException(e, "workflow", state);
		}
		return false;
	}

	/**
	 * 从数据库中查找该角色Role所拥有的Function.
	 * 
	 * @param authority
	 *            the authority
	 * @param state
	 *            the state
	 * @return the role
	 */

	public Role findgrantedAuthorityFromDataBase(String authority, State state) {
		try {
			return (Role) getDelegate().execute("authority", authority, ServiceConsts.FindgrantedAuthorityFromDataBaseProcessor,
					"resultObject", state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findgrantedAuthorityFromDataBase", state);
			return null;
		}
	}

	/**
	 * 从数据库中查找名为protectFunction的Function以及拥有这个function的角色.
	 * 
	 * @param protectfunction
	 *            the protectfunction
	 * @param state
	 *            the state
	 * @return the function
	 */

	public Function findFunctionFromDataBase(String protectfunction, State state) {
		try {
			return (Function) getDelegate().execute("protectfunction", protectfunction,
					ServiceConsts.FindFunctionFromDataBaseProcessor, "resultObject", state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findFunctionFromDataBase", state);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#findRoleByFunction(java.lang.
	 * String, com.legendshop.command.framework.State)
	 */

	public List findRoleByFunction(Long functionId, State state) {

		try {
			return (List) getDelegate()
					.execute("functionId", functionId, ServiceConsts.FindRoleByFunctionProcessor, "roles", state);
		} catch (Exception e) {
			throw new ClientException(e, "findRoleByFunction error");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#findRoleByUser(java.lang.String,
	 * com.legendshop.command.framework.State)
	 */

	public List findRoleByUser(Long userId, String appNo, State state) {
		try {
			Request req = new Request();
			req.setValue("userId", userId);
			req.setValue("appNo", appNo);
			req.setServiceName(ServiceConsts.FindRoleByUserProcessor);
			Response resp = getDelegate().execute(req);
			return ((List) resp.getValue("roles"));
			
			//return (List) getDelegate().execute("userId", userId, ServiceConsts.FindRoleByUserProcessor, "roles", state);
			
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findRoleByUser", state);
			return null;
		}
	}

	/**
	 * 从数据库中查找该用户User以及所拥有的role.
	 * 
	 * @param name
	 *            the name
	 * @param state
	 *            the state
	 * @return the user
	 */

	public User findUserByNameFromDataBase(String name, State state) {
		try {
			return (User) getDelegate().execute("name", name, ServiceConsts.FindUserByNameFromDataBaseProcessor, "resultObject",
					state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findUserByNameFromDataBase", state);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#saveFunction(com.legendshop.model
	 * .entity.Function, com.legendshop.command.framework.State)
	 */

	public Long saveFunction(Function function, State state) {
		try {
			return (Long) getDelegate().execute("function", function, ServiceConsts.SaveFunctionProcessor, "id", state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "saveFunction", state);
			return null;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#saveFunctionToRole(com.legendshop
	 * .model.entity.Permission, com.legendshop.command.framework.State)
	 */

	public boolean saveFunctionToRole(Permission permission, State state) {
		try {
			getDelegate().execute("permission", permission, ServiceConsts.SaveFunctionToRoleProcessor, "id", state);
			return true;
		} catch (Exception e) {
			DelegateUtil.handleException(e, "saveFunctionToRole", state);
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#saveFunctionsToRole(java.util
	 * .List, com.legendshop.command.framework.State)
	 */

	public void saveFunctionsToRole(List permissions, State state) {
		if (DelegateUtil.isNullParam(permissions, "permissions", state)) {
			return;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.SaveFunctionsToRoleProcessor);
		req.setValue("permissions", permissions);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "saveFunctionsToRole", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#updateFunction(com.legendshop
	 * .model.entity.Function, com.legendshop.command.framework.State)
	 */

	public void updateFunction(Function function, State state) {
		if (DelegateUtil.isNullParam(function, "function", state)) {
			return;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.UpdateFunctionProcessor);
		req.setValue("function", function);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "updateFunction", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#isUserExist(java.lang.String,
	 * com.legendshop.command.framework.State)
	 */

	public boolean isUserExist(String name, State state) {
		if (DelegateUtil.isNullParam(name, "name", state)) {
			return false;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.IsUserExistProcessor);
		req.setValue("name", name);

		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return ((Boolean) resp.getValue("resultBoolean")).booleanValue();
		} catch (Exception e) {
			DelegateUtil.handleException(e, "isUserExist", state);
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#saveUser(com.legendshop.model
	 * .entity.User, com.legendshop.command.framework.State)
	 */

	public Long saveUser(User user, State state) {
		try {
			return (Long) getDelegate().execute("user", user, ServiceConsts.SaveUserProcessor, "result", state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "saveUser", state);
			return null;
		}

	}


	/**
	 * 根据FunctionId删除功能点，同时删除function和role的对应关系 deletePermissionByFunctionId.
	 * 
	 * @param functionId
	 *            the function id
	 * @param state
	 *            the state
	 * @return true, if successful
	 */

	public boolean deleteFunctionById(Long functionId, State state) {
		if (DelegateUtil.isNullParam(functionId, "functionId", state)) {
			return false;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.DeleteFunctionByIdProcessor);
		req.setValue("functionId", functionId);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return ((Boolean) resp.getValue("resultBoolean")).booleanValue();
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteFunctionById", state);
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deleteFunction(com.legendshop
	 * .model.entity.Function, com.legendshop.command.framework.State)
	 */

	public void deleteFunction(Function function, State state) {
		if (DelegateUtil.isNullParam(function, "function", state)) {
			return;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.DeleteFunctionProcessor);
		req.setValue("function", function);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteFunction", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#findFunctionById(java.lang.String
	 * , com.legendshop.command.framework.State)
	 */

	public Function findFunctionById(Long id, State state) {
		if (DelegateUtil.isNullParam(id, "id", state)) {
			return null;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.FindFunctionByIdProcessor);
		req.setValue("id", id);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			Function function = (Function) resp.getValue("function");
			DelegateUtil.setState(state, resp);
			return function;
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findFunctionById", state);
			return null;
		}
	}

	/**
	 * 查找该用户的所有权限.
	 * 
	 * @param userId
	 *            the user id
	 * @param state
	 *            the state
	 * @return the list
	 */

	public List findFunctionByUser(Long userId, String appNo, State state) {
		try {
			Request req = new Request();
			req.setValue("userId", userId);
			req.setValue("appNo", appNo);
			
			req.setServiceName(ServiceConsts.FindFunctionByUserProcessor);
			Response resp = getDelegate().execute(req);
			
			DelegateUtil.setState(state, resp);
			List functions = (List) resp.getValue("functions");
			DelegateUtil.setState(state, resp);
			return functions;
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findFunctionByUser", state);
			return null;
		}
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#saveRole(com.legendshop.model
	 * .entity.Role, com.legendshop.command.framework.State)
	 */

	public Long saveRole(Role role, State state) {
		if (DelegateUtil.isNullParam(role, "role", state)) {
			return null;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.SaveRoleProcessor);
		req.setValue("role", role);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return ((Long) resp.getValue("result"));
		} catch (Exception e) {
			DelegateUtil.handleException(e, "saveRole", state);
			return null;
		}
	}

	/**
	 * 删除角色，同时删除该角色对应的权限关系.deletePermissionByRoleId
	 * 
	 * @param roleId
	 *            the role id
	 * @param state
	 *            the state
	 */

	public void deleteRoleById(Long roleId, State state) {
		if (DelegateUtil.isNullParam(roleId, "roleId", state)) {
			return;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.DeleteRoleByIdProcessor);
		req.setValue("roleId", roleId);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteRoleById", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deleteRole(com.legendshop.model
	 * .entity.Role, com.legendshop.command.framework.State)
	 */

	public void deleteRole(Role role, State state) {
		if (DelegateUtil.isNullParam(role, "role", state)) {
			return;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.DeleteRoleProcessor);
		req.setValue("role", role);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteRole", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#updateRole(com.legendshop.model
	 * .entity.Role, com.legendshop.command.framework.State)
	 */

	public void updateRole(Role role, State state) {
		if (DelegateUtil.isNullParam(role, "role", state)) {
			return;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.UpdateRoleProcessor);
		req.setValue("role", role);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "updateRole", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#updateUser(com.legendshop.model
	 * .entity.User, com.legendshop.command.framework.State)
	 */

	public void updateUser(User user, State state) {
		try {
			getDelegate().execute("user", user, ServiceConsts.UpdateUserProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "updateUser", state);
		}
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#findRoleById(java.lang.String,
	 * com.legendshop.command.framework.State)
	 */

	public Role findRoleById(Long id, State state) {
		if (DelegateUtil.isNullParam(id, "id", state)) {
			return null;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.FindRoleByIdProcessor);
		req.setValue("id", id);
		try {
			Response resp = getDelegate().execute(req);
			Role role = (Role) resp.getValue("role");
			DelegateUtil.setState(state, resp);
			return role;
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findRoleById", state);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#findFunctionByRoleId(java.lang
	 * .String, com.legendshop.command.framework.State)
	 */

	public List<Function> findFunctionByRoleId(Long roleId, State state) {
		if (DelegateUtil.isNullParam(roleId, "roleId", state)) {
			return null;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.FindFunctionByRoleIdProcessor);
		req.setValue("roleId", roleId);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return (List<Function>) resp.getValue("functions");
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findFunctionByRoleId", state);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#findOtherFunctionByRoleId(java
	 * .lang.String, com.legendshop.command.framework.State)
	 */

	public List findOtherFunctionByRoleId(Long roleId, State state) {
		if (DelegateUtil.isNullParam(roleId, "roleId", state)) {
			return null;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.FindOtherFunctionByRoleIdProcessor);
		req.setValue("roleId", roleId);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return (List) resp.getValue("functions");
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findOtherFunctionByRoleId", state);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deleteFunctionsFromRole(java.
	 * util.List, com.legendshop.command.framework.State)
	 */

	public void deleteFunctionsFromRole(List<Permission> permissions, State state) {
		try {
			getDelegate().execute("permissions", permissions, ServiceConsts.DeleteFunctionsFromRoleProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteFunctionsFromRole", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deleteRoleFromUser(java.util.
	 * List, com.legendshop.command.framework.State)
	 */

	public void deleteRoleFromUser(List userRoles, State state) {
		try {
			getDelegate().execute("userRoles", userRoles, ServiceConsts.DeleteRoleFromUserProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteRoleFromUser", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#findUserById(java.lang.String,
	 * com.legendshop.command.framework.State)
	 */

	public User findUserById(Long userId, State state) {
		try {
			return (User) getDelegate().execute("userId", userId, ServiceConsts.FindUserByIdProcessor, "user", state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findUserById", state);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#saveRoleToUser(com.legendshop
	 * .model.entity.UserRole, com.legendshop.command.framework.State)
	 */

	public void saveRoleToUser(UserRole userRole, State state) {
		try {
			if (!AppUtils.isBlank(userRole) && !AppUtils.isBlank(userRole.getId().getUserId())
					&& !AppUtils.isBlank(userRole.getId().getRoleId())) {
				List userRoles = new ArrayList();
				userRoles.add(userRole);
				saveRolesToUser(userRoles, state);
			} else {
				throw new JCFException(state.getErrCode(), "saveRoleToUser userRole is not validated!");
			}
		} catch (Exception e) {
			DelegateUtil.handleException(e, "saveRoleToUser", state);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#updateUserPassowrd(java.lang.
	 * String, java.lang.String, com.legendshop.command.framework.State)
	 */

	public void updateUserPassowrd(Long userId, String password, State state) {
		if (DelegateUtil.isNullParam(userId, "userId", state) || DelegateUtil.isNullParam(password, "password", state)) {
			return;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.UpdateUserPassowrdProcessor);
		req.setValue("userId", userId);
		req.setValue("password", password);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "updateUserPassowrd", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deletePermissionByFunctionId(
	 * java.lang.String, com.legendshop.command.framework.State)
	 */

	public void deletePermissionByFunctionId(Long functionId, State state) {
		try {
			getDelegate().execute("functionId", functionId, ServiceConsts.DeletePermissionByFunctionIdProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deletePermissionByFunctionId", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deleteUserRoleByUserId(java.lang
	 * .String, com.legendshop.command.framework.State)
	 */

	public void deleteUserRoleByUserId(Long userId, State state) {
		try {
			getDelegate().execute("userId", userId, ServiceConsts.DeleteUserRoleByUserIdProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteUserRoleByUserId", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deleteRoleFromUser(com.legendshop
	 * .model.entity.UserRole, com.legendshop.command.framework.State)
	 */

	public void deleteRoleFromUser(UserRole userRole, State state) {
		if (AppUtils.isBlank(userRole))
			throw new ClientException(ErrorCode.PARAMETER_ERROR, "deleteRoleFromUser");
		List userRoles = new ArrayList();
		userRoles.add(userRole);
		deleteRoleFromUser(userRoles, state);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deleteUserRoleByRoleId(java.lang
	 * .String, com.legendshop.command.framework.State)
	 */

	public void deleteUserRoleByRoleId(Long roleId, State state) {
		try {
			getDelegate().execute("roleId", roleId, ServiceConsts.DeleteUserRoleByRoleIdProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteUserRoleByRoleId", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deletePermissionByRoleId(java
	 * .lang.String, com.legendshop.command.framework.State)
	 */

	public void deletePermissionByRoleId(Long roleId, State state) {
		try {
			getDelegate().execute("roleId", roleId, ServiceConsts.DeletePermissionByRoleIdProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deletePermissionByRoleId", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#saveRolesToUser(java.util.List,
	 * com.legendshop.command.framework.State)
	 */

	public void saveRolesToUser(List userRoles, State state) {
		try {
			getDelegate().execute("userRoles", userRoles, ServiceConsts.SaveRolesToUserProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "saveRolesToUser", state);
		}
	}

	// ablout File. Create in 2007-06-30 17:23:25.
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.legendshop.permission.RightDelegate#saveFile(java.io.File,
	 * com.legendshop.command.framework.State)
	 */

	public String saveFile(File file, State state) {
		try {
			return (String) getDelegate().execute("file", file, ServiceConsts.SaveFileProcessor, "result", state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "saveFile", state);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#deleteFileById(java.lang.String,
	 * com.legendshop.command.framework.State)
	 */

	public void deleteFileById(String id, State state) {
		try {
			getDelegate().execute("id", id, ServiceConsts.DeleteFileByIdProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteFileById", state);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.legendshop.permission.RightDelegate#deleteFile(java.io.File,
	 * com.legendshop.command.framework.State)
	 */

	public void deleteFile(File file, State state) {
		try {
			getDelegate().execute("file", file, ServiceConsts.DeleteFileProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "deleteUserRoleByUserId", state);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.legendshop.permission.RightDelegate#updateFile(java.io.File,
	 * com.legendshop.command.framework.State)
	 */

	public void updateFile(File file, State state) {
		try {
			getDelegate().execute("file", file, ServiceConsts.UpdateFileProcessor, state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "updateFile", state);
		}
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#findFileById(java.lang.String,
	 * com.legendshop.command.framework.State)
	 */

	public File findFileById(String id, State state) {
		try {
			return (File) getDelegate().execute("id", id, ServiceConsts.FindFileByIdProcessor, "file", state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findFileById", state);
			return null;
		}
	}

	// ablout File. Create in 2007-06-30 17:23:25.
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#saveLoginHistory(com.legendshop
	 * .model.entity.LoginHistory, com.legendshop.command.framework.State)
	 */

	public String saveLoginHistory(LoginHistory loginHistory, State state) {
		try {
			return (String) getDelegate().execute("loginHistory", loginHistory, ServiceConsts.SaveLoginHistoryProcessor, "result",
					state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "saveLoginHistory", state);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.RightDelegate#testCircle(com.legendshop.command
	 * .framework.State)
	 */

	public String testCircle(State state) {
		try {
			return (String) getDelegate().execute("testCircleProcessor", "next", state);
		} catch (Exception e) {
			DelegateUtil.handleException(e, "testCircle", state);
			return null;
		}
	}

	@Override
	public PageSupport<Role> findAllRolePage(String curPageNO, Role role, State state) {
		// Qbc查找方式
		CriteriaQuery cq = new CriteriaQuery(Role.class, curPageNO);
		cq.setPageSize(PropertiesUtil.getObject(SysParameterEnum.PAGE_SIZE, Integer.class));
		String name = role.getName();
		String enabled = role.getEnabled();
		Map<String,Object> paraMap=new HashMap<>();

		if (!AppUtils.isBlank(name)) {
			cq.like("name", "%" + name + "%");
			paraMap.put("name", name);
		}
		if (AppUtils.isNotBlank(enabled)) {
			cq.eq("enabled", enabled);
			paraMap.put("enabled", enabled);
		}
		cq.eq("appNo", role.getAppNo());
		paraMap.put("appNo", role.getAppNo());
		
		cq.eq("category", role.getCategory());
		paraMap.put("category",role.getCategory());
		
		cq.addOrder("desc","roleType");
				
		if (DelegateUtil.isNullParam(cq, "CriteriaQuery", state)) {
			return null;
		}
		Request req = new Request();
		req.setValues(paraMap);
		req.setServiceName(ServiceConsts.FindAllRoleProcessor);
		req.setValue("CriteriaQuery", cq);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return (PageSupport) (resp.getValue("PageSupport"));
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findAllRole", state);
			return null;
		}	
	}

	@Override
	public PageSupport<Function> findOtherFunctionByHql(String curPageNO, Long roleId, State state) {
		
		// HQL查找方式
		SimpleSqlQuery hqlQuery = new SimpleSqlQuery(Function.class);
		hqlQuery.setCurPage(curPageNO);
		hqlQuery.setPageSize(PropertiesUtil.getObject(SysParameterEnum.PAGE_SIZE, Integer.class));
		if (DelegateUtil.isNullParam(hqlQuery, "hqlQuery", state)) {
			return null;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.FindOtherFunctionByHqlProcessor);
		req.setValue("hqlQuery", hqlQuery);
		req.setValue("roleId", roleId);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return (PageSupport) resp.getValue("functions");
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findOtherFunctionByHql", state);
			return null;
		}
	}

	@Override
	public PageSupport<Role> findAllRole(String search, String myaction, String curPageNO, State state) {
		if (DelegateUtil.isNullParam(search, "search", state)) {
			return null;
		}
		Request req = new Request();
		req.setServiceName(ServiceConsts.FindAllRoleProcessor);
		req.setValue("search", search);
		req.setValue("curPageNO", curPageNO);
		req.setValue("myaction", myaction);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return (PageSupport) (resp.getValue("PageSupport"));
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findAllRole", state);
			return null;
		}
	}


	@Override
	public PageSupport<Function> findAllFunction(String curPageNO, Function function, State state) {
		Request req = new Request();
		req.setServiceName(ServiceConsts.FindAllFunctionProcessor);
		req.setValue("curPageNO", curPageNO);
		req.setValue("function", function);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return (PageSupport) (resp.getValue("PageSupport"));
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findAllFunction", state);
			return null;
		}
	}


	
	/**
	 * 查找该用户的还没有授权的角色，用来增加角色.
	 * 
	 */
	@Override
	public PageSupport<Role> findOtherRoleByUser(String curPageNO, Long userId, String appNo, State state) {
		Request req = new Request();
		req.setServiceName(ServiceConsts.FindOtherRoleByUserProcessor);
		req.setValue("curPageNO", curPageNO);
		req.setValue("userId", userId);
		req.setValue("appNo", appNo);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return (PageSupport) resp.getValue("roles");
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findOtherRoleByUser", state);
			return null;
		}
	}

	@Override
	public PageSupport<User> findAllUser(String curPageNO, String name, String enabled, State state) {
		Request req = new Request();
		req.setServiceName(ServiceConsts.FindAllUserProcessor);
		req.setValue("curPageNO", curPageNO);
		req.setValue("name", name);
		req.setValue("enabled", enabled);
		try {
			Response resp = getDelegate().execute(req);
			DelegateUtil.setState(state, resp);
			return (PageSupport) (resp.getValue("PageSupport"));
		} catch (Exception e) {
			DelegateUtil.handleException(e, "findAllUser", state);
			return null;
		}

	}

}
